// 根据角色动态生成路由
import { defineFakeRoute } from "vite-plugin-fake-server/client";

export default defineFakeRoute([
  // 新的登录API
  {
    url: "/api/auth/login",
    method: "post",
    response: ({ body }) => {
      if (
        body.usernameOrEmail === "admin" ||
        body.usernameOrEmail === "admin@example.com"
      ) {
        return {
          code: 0,
          message: "success",
          data: {
            accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.admin",
            user: {
              id: 1,
              username: "admin",
              email: "admin@example.com",
              nickname: "管理员",
              avatar: "https://avatars.githubusercontent.com/u/44761321",
              isActive: true,
              createdAt: "2024-01-01T00:00:00.000Z",
              updatedAt: "2024-01-01T00:00:00.000Z"
            }
          },
          timestamp: new Date().toISOString()
        };
      } else if (
        body.usernameOrEmail === "common" ||
        body.usernameOrEmail === "common@example.com"
      ) {
        return {
          code: 0,
          message: "success",
          data: {
            accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.common",
            user: {
              id: 2,
              username: "common",
              email: "common@example.com",
              nickname: "普通用户",
              avatar: "https://avatars.githubusercontent.com/u/52823142",
              isActive: true,
              createdAt: "2024-01-01T00:00:00.000Z",
              updatedAt: "2024-01-01T00:00:00.000Z"
            }
          },
          timestamp: new Date().toISOString()
        };
      } else {
        return {
          code: 1001,
          message: "用户名或密码错误",
          data: null,
          timestamp: new Date().toISOString()
        };
      }
    }
  },
  // 保留原有的登录API以兼容
  {
    url: "/login",
    method: "post",
    response: ({ body }) => {
      if (body.username === "admin") {
        return {
          success: true,
          data: {
            avatar: "https://avatars.githubusercontent.com/u/44761321",
            username: "admin",
            nickname: "小铭",
            // 一个用户可能有多个角色
            roles: ["admin"],
            // 按钮级别权限
            permissions: ["*:*:*"],
            accessToken: "eyJhbGciOiJIUzUxMiJ9.admin",
            refreshToken: "eyJhbGciOiJIUzUxMiJ9.adminRefresh",
            expires: "2030/10/30 00:00:00"
          }
        };
      } else {
        return {
          success: true,
          data: {
            avatar: "https://avatars.githubusercontent.com/u/52823142",
            username: "common",
            nickname: "小林",
            roles: ["common"],
            permissions: ["permission:btn:add", "permission:btn:edit"],
            accessToken: "eyJhbGciOiJIUzUxMiJ9.common",
            refreshToken: "eyJhbGciOiJIUzUxMiJ9.commonRefresh",
            expires: "2030/10/30 00:00:00"
          }
        };
      }
    }
  }
]);
